package com.sphd.education.adapter;

import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.sphd.education.R;
import com.sphd.education.utils.ToastUtils;

import net.sf.json.JSONArray;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by hu on 2016/2/26.
 */
public class TagsAdapter extends BaseAdapter {
    private List<String> listtags;
    int selectNo=0;
    List<String> stringList ;

    public List<String> getStringList() {
        return stringList;
    }

    public void setStringList(List<String> stringList) {
        this.stringList = stringList;
    }

    public TagsAdapter(List<String> listtags) {
        this.listtags =listtags;
        stringList=new ArrayList<>();
    }

    @Override
    public int getCount() {
        return listtags.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        ViewHolder viewHolder=null;
        if (convertView == null) {
            convertView = View.inflate(parent.getContext(), R.layout.tags_item, null);
            viewHolder=new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else {
            viewHolder= (ViewHolder) convertView.getTag();
        }
        viewHolder.tagsTv.setText(listtags.get(position));

        final ViewHolder finalViewHolder = viewHolder;
        viewHolder.tagsTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("selectNo1",selectNo+"");
                if (!finalViewHolder.tagsTv.isChecked()){
                    finalViewHolder.tagsTv.setChecked(false);
                    selectNo--;
                    stringList.remove(listtags.get(position)+",");
                }else {
                    if (selectNo<=5){
                        selectNo++;
                        stringList.add(listtags.get(position)+",");
                        finalViewHolder.tagsTv.setChecked(true);
                    }else {
                        finalViewHolder.tagsTv.setChecked(false);
                        ToastUtils.MyToast(parent.getContext(),"选择已超出上限");
                    }
                }
                Log.e("selectNo2",selectNo+"");
            }
        });
        return convertView;
    }

    static class ViewHolder {
        @Bind(R.id.tags_tv)
        CheckBox tagsTv;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
